package at.fhooe.mc.sn.gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import at.fhooe.mc.sn.ProtocolHandler;

public class Temp extends JPanel implements ActionListener{
	
	ProtocolHandler protocol;
	private JLabel temp;
	private JLabel minV;
	private JLabel maxV;
	private JLabel loThresV;
	private JLabel upThresV;
	private JLabel dtV;

	/**
	 * Create the panel.
	 * @param protocol 
	 */
	public Temp(ProtocolHandler protocol) {
		
		this.protocol = protocol;
		
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(23, 55, 353, 164);
		add(panel);
		panel.setLayout(new GridLayout(6, 2, 0, 0));
		
		JLabel lblTemp = new JLabel("Temp");
		panel.add(lblTemp);
		lblTemp.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		temp = new JLabel("");
		panel.add(temp);
		temp.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		JLabel min = new JLabel("min");
		min.setFont(new Font("Tahoma", Font.PLAIN, 17));
		panel.add(min);
		
		minV = new JLabel("");
		minV.setFont(new Font("Tahoma", Font.PLAIN, 17));
		panel.add(minV);
		
		JLabel max = new JLabel("max");
		panel.add(max);
		max.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		maxV = new JLabel("");
		panel.add(maxV);
		maxV.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		JLabel lblX = new JLabel("lower thres.");
		panel.add(lblX);
		lblX.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		loThresV = new JLabel("");
		panel.add(loThresV);
		loThresV.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		JLabel lblZ = new JLabel("upper thres.");
		panel.add(lblZ);
		lblZ.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		upThresV = new JLabel("");
		upThresV.setFont(new Font("Tahoma", Font.PLAIN, 17));
		panel.add(upThresV);
		
		JLabel lblDtdt = new JLabel("dT/dt");
		lblDtdt.setFont(new Font("Tahoma", Font.PLAIN, 17));
		panel.add(lblDtdt);
		
		dtV = new JLabel("");
		dtV.setFont(new Font("Tahoma", Font.PLAIN, 17));
		panel.add(dtV);
		
		JLabel lblAccelerometer = new JLabel("Temperature");
		lblAccelerometer.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblAccelerometer.setBounds(140, 11, 159, 29);
		add(lblAccelerometer);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.setBounds(351, 11, 89, 23);
		add(btnRefresh);
		btnRefresh.addActionListener(this);
		
		refresh();
		
		setVisible(true);
	}
	
	public void refresh(){
		temp.setText(String.valueOf(protocol.requestDouble("REQTEMP")));
		minV.setText(String.valueOf(protocol.requestDouble("REQPARA:21")));
		maxV.setText(String.valueOf(protocol.requestDouble("REQPARA:20")));
		loThresV.setText(String.valueOf(protocol.requestDouble("REQPARA:2")));
		upThresV.setText(String.valueOf(protocol.requestDouble("REQPARA:1")));
		dtV.setText(String.valueOf(protocol.requestDouble("REQPARA:3")));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		refresh();
	}
}
